package edu.govschool2010.shared;

import java.io.*;
import java.util.*;

import android.app.Activity;
import android.content.Context;

public class WriteToFile extends Activity {

	private static WriteToFile instance = null;

private WriteToFile(){
	
}

public static WriteToFile getInstanceW(){

	if (instance == null){
		instance = new WriteToFile();
	}
	return instance;
}
	
public boolean deleteSituation(ArrayList<Situation> list, int sit) throws java.io.IOException{ //will delete file and return whether the file was successfully deleted or not
	//need to call getInstanceW() first
	boolean deleted = false;
	try{
		String FILE_NAME = list.get(sit).situationName + ".txt";
		 deleted = deleteFile(FILE_NAME);
	}
	catch(SecurityException sec){
		deleted = false;
	}
	finally{
		
	}
	return deleted;
}

public static void changePrioTo (ArrayList<Situation> list, int oldPrio, int newPrio){ //Input the ArrayList, priority you want including zero
	Situation temp = new Situation("temp");
	temp = list.get(oldPrio);
	list.remove(oldPrio);
	list.add(newPrio,temp);

}

public void saveChanges (ArrayList <Situation> list) throws java.io.IOException { //need to call getInstanceW() first
	FileOutputStream fos = null;
	FileOutputStream fos2 = null;
	PrintWriter pw = new PrintWriter(fos, true);
	PrintWriter pw2 = new PrintWriter(fos2, true);
	try{
		for (int x =0; x<= list.size()-1; x++){
			String FILE_NAME = list.get(x).situationName + ".txt";
			
			
			fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
			pw = new PrintWriter(fos, true);
			pw.println(list.get(x).situationName);
			pw.println(list.get(x).awayMessageText);		
			pw.println(list.get(x).startTime);
			pw.println(list.get(x).endTime);

			pw.println(list.get(x).wifi);
			pw.println(list.get(x).bluetooth);
			pw.println(list.get(x).gps);
			pw.println(list.get(x).airplaneMode);
			pw.println(list.get(x).awayMessage);
			pw.println(list.get(x).volume);
			pw.println(list.get(x).ringerMode);
			pw.println(list.get(x).brightness);
			for (int y = 0; y<=6; y++){
			pw.println(list.get(x).daysOfWeek[y]);
			}
			pw.println(list.get(x).loc);
			pw.println(list.get(x).contact);
		}
		for (int x = 0; x<= list.size()-1;x++){
			fos2 = openFileOutput("Priority.txt", Context.MODE_PRIVATE);
			pw2 = new PrintWriter (fos2, true);
			pw2.println(list.get(x).situationName);
		}
		
		pw.flush();
		pw2.flush();
		
		
	}
	finally{
		pw.close();
	}
}
}

